{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "## Buttons and LEDs demonstration\n",
    "\n",
    "This demo shows how to use push-buttons (BTN0-3), LEDs (LD0-3), and RGB LEDs (LD4-5) on the board. \n",
    "\n",
    "You can do the following to control the LEDs or RGB LEDs:\n",
    "    \n",
    "    Button 0 pressed:        RGB LEDs change color.\n",
    "    Button 1 pressed:        LEDs shift from right to left (LD0 -> LD3).\n",
    "    Button 2 pressed:        LEDs shift from left to right (LD3 -> LD0).\n",
    "    Button 3 pressed:        Turns off all the LEDS and ends this demo.\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "End of this demo ...\n"
     ]
    }
   ],
   "source": [
    "from time import sleep\n",
    "from pynq.overlays.base import BaseOverlay\n",
    "\n",
    "base = BaseOverlay(\"base.bit\")\n",
    "\n",
    "Delay1 = 0.3\n",
    "Delay2 = 0.1\n",
    "color = 0\n",
    "rgbled_position = [4,5]\n",
    "\n",
    "for led in base.leds:\n",
    "    led.on()    \n",
    "while (base.buttons[3].read()==0):\n",
    "    if (base.buttons[0].read()==1):\n",
    "        color = (color+1) % 8\n",
    "        for led in rgbled_position:\n",
    "            base.rgbleds[led].write(color)\n",
    "            base.rgbleds[led].write(color)\n",
    "        sleep(Delay1)\n",
    "        \n",
    "    elif (base.buttons[1].read()==1):\n",
    "        for led in base.leds:\n",
    "            led.off()\n",
    "        sleep(Delay2)\n",
    "        for led in base.leds:\n",
    "            led.toggle()\n",
    "            sleep(Delay2)\n",
    "            \n",
    "    elif (base.buttons[2].read()==1):\n",
    "        for led in reversed(base.leds):\n",
    "            led.off()\n",
    "        sleep(Delay2)\n",
    "        for led in reversed(base.leds):\n",
    "            led.toggle()\n",
    "            sleep(Delay2)                  \n",
    "    \n",
    "print('End of this demo ...')\n",
    "for led in base.leds:\n",
    "    led.off()\n",
    "for led in rgbled_position:\n",
    "    base.rgbleds[led].off()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
